package com.aqarat.managedBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.aqarat.controller.AqaratController;
import com.aqarat.controller.AqaratControllerImpl;
import com.aqarat.entity.Apartment;
import com.aqarat.entity.Customer;
import com.core.managedBean.BaseBean;

/**
 * @author karimsherif
 * 
 */
@ManagedBean
@ViewScoped
public class SaleApartmentBean extends BaseBean {
	private static final long serialVersionUID = 1L;
	private Apartment apartment;
	private AqaratController aqaratController;

	@PostConstruct
	public void loadData() {
		if (aqaratController == null) {
			aqaratController = new AqaratControllerImpl();
		}
		loadApartmentDetails();
		// loadCustomerList();
	}

	public void loadApartmentDetails() {
		Object o = super.getFlashAttribute("apartment");
		System.out.println(o);
		if (o != null) {
			apartment = (Apartment) o;
		} else {
			apartment = new Apartment();
		}

	}

	public List<Customer> searchCustomer(String customerName) {
		try {
			return aqaratController.getCustomerListByName(customerName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public void saleApartment() {
		try {
			aqaratController.saleApartment(apartment);
			super.addInfoMessage("Successfully Saled");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// =================================//
	public final Apartment getApartment() {
		return apartment;
	}

	public final void setApartment(Apartment apartment) {
		this.apartment = apartment;
	}

	public final AqaratController getAqaratController() {
		return aqaratController;
	}

	public final void setAqaratController(AqaratController aqaratController) {
		this.aqaratController = aqaratController;
	}
}
